<?php
/**
 * Object Relational Persistence Mapping Class for table: encounter_room
 *
 * @package	com.clearhealth
 * @author	ClearHealth Inc.
 */
class QueueRoom extends ORDataObject {

	/**#@+
	 * Fields of table: encounter_room mapped to class members
	 */
	var $room_id		= '';
	var $type = '';
	/**#@-*/

	const ENCOUNTER = "encounter";
	const CASHIER = "cashier";
	const LABORATORY = "laboratory";

	/**
	 * DB Table
	 */
	var $_table = 'queue_room';

	/**
	 * Primary Key
	 */
	var $_key = 'Array';
	
	/**
	 * Internal Name
	 */
	var $_internalName = 'QueueRoom';

	/**
	 * Handle instantiation
	 */
	function QueueRoom() {
		parent::ORDataObject();
	}

	public function add($roomId, $type) {
		$data = array('room_id' => $roomId, 'type' => $type);			
		$this->populateArray($data);
		$this->persist();
	}
		
	public function update($roomId, $type, $save = false) {
		$existance = $this->isExist($roomId, $type);
		if ($existance && !$save) {
			$this->delete($roomId);
		} else if (!$existance && $save) {
			$this->add($roomId, $type);
		}
	}
	
	public function delete($roomId) {
		$sql = "DELETE FROM $this->_table WHERE room_id = $roomId";
		$this->_execute($sql);
	}
	
	public function getQueueRooms($departmentId, $personId) {
	    $field     =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
		$ds 	=	new Datasource_sql();		
		$sql 	=	array(
			'cols'		=>	"R.room_id, R.$field name",
			'from'		=> 	"$this->_table QR
				INNER JOIN room R ON QR.room_id = R.room_id
				INNER JOIN room_employee_map RE ON RE.room_id = R.room_id",
			'where'		=> 	"R.department_id = $departmentId AND RE.person_id = $personId",
			'orderby'	=>	"R.department_id"
		);
		$ds->setup($this->_db, $sql);
		return $ds->toArray('room_id', 'name');
	}
	
	public function getQueueRoomIds($departmentId, $personId) {
		$ds 	=	new Datasource_sql();
		$field 	=	L10nHelper::getLocale();
		$sql 	=	array(
			'cols'		=>	"R.room_id",
			'from'		=> 	"$this->_table QR
				INNER JOIN room R ON QR.room_id = R.room_id
				INNER JOIN room_employee_map RE ON RE.room_id = R.room_id",
			'where'		=> 	"R.department_id = $departmentId AND RE.person_id = $personId",
			'orderby'	=>	"R.department_id"
		);
		$ds->setup($this->_db, $sql);
		return $ds->toArray('room_id');
	}
	
	public function getAll($type = array(), $departmentId = false) {
	    $field     =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
		
		$ds = new Datasource_sql();
		$sql = array(
			'cols'	=> "R.room_id, R.$field name",
			'from'	=> "$this->_table QR 
				INNER JOIN room R ON QR.room_id = R.room_id",			
			'orderby'	=> "R.department_id"
		);
		
		$count = sizeof($type);
		if ($count > 0) {
			$sql['where'] = 'false';
			foreach ($type as $queueType) {
				$sql['where'] .= " OR QR.type = '$queueType'";
			}
		}				
		
		$ds->setup($this->_db, $sql);
		
		return $ds->toArray('room_id', 'name');
	}
	
	public function isExist($roomId, $type = '') {
		$sql = "SELECT room_id FROM $this->_table WHERE room_id = $roomId";
		if (!empty($type)) {
			$sql .= " AND type ='" . $type . "'";
		}
		$res = $this->_execute($sql);
		if ($res && !$res->EOF) {
			return true;
		}
		return false;
	}
	
	public function getWorkingRoom($personId) {
		$ds = new Datasource_sql();
		$field = L10nHelper::getLocale();
		$sql = array(
			'cols'	=> "R.room_id, R.$field name",
			'from'	=> "room_employee_map REM 
			             LEFT JOIN room R ON REM.room_id = R.room_id",			
			'orderby' => "name",
			'where' => "REM.person_id = $personId" 
		);
		$ds->setup($this->_db, $sql);
		return $ds->toArray('room_id', 'name');
	}
}
?>
